package com.gljx.web.service.impl;

import com.gljx.util.CommonExport;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.SubjectOneResult;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.SubjectOneResultMapper;
import com.gljx.web.service.SubjectOneResultService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SubjectOneResultServiceImpl extends CommonDao implements SubjectOneResultService {
	private static String[] cloumn = {"学员姓名","学习驾驶证明编号","身份证号码","考试科目","考试车型","考试日期","考试场地","考试次数","考试成绩"};
	@Autowired
	private SubjectOneResultMapper subjectOneResultMapper;
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectOneResultMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectOneResultMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectOneResultMapper.selectFnumber(companyId);
	}

	@Override
	public int addSubjectOneResult(SubjectOneResult subjectOneResult) {
		// TODO Auto-generated method stub
		return subjectOneResultMapper.insertSelective(subjectOneResult);
	}

	@Override
	public int deleteSubjectOneResult(String fid) {
		// TODO Auto-generated method stub
		return subjectOneResultMapper.deleteByPrimaryKey(fid);
	}

	
	@Override
	public SubjectOneResult selectSubjectOneResultByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectOneResultMapper.selectSubjectOneResultByFid(fid);
	}

	@Override
	public int updateSubjectOneReusltByFid(SubjectOneResult subjectOneResult) {
		// TODO Auto-generated method stub
		return subjectOneResultMapper.updateByPrimaryKeySelective(subjectOneResult);
	}

	@Override
	public void importExcelSubjectOneResult(Workbook wb, User user, HttpServletRequest request) {
		// TODO Auto-generated method stub
		StringBuilder failMessage = new StringBuilder();
		StringBuilder errorinfo = new StringBuilder();
		boolean validResult = true;
		int successNum = 0;
		int failNum = 0;
		//验证第一行列名正确性		开始
		Sheet sheet=wb.getSheetAt(0);
		Row headRow = sheet.getRow(0);
		if(headRow!=null){
			if(cloumn.length==headRow.getLastCellNum()){//列数量匹配
				for (int col = 0; col < headRow.getLastCellNum(); col++) {
					if(!cloumn[col].equals(headRow.getCell(col).getStringCellValue().trim())){
						validResult = false;
					}
				}
			}else{
				validResult = false;
			}
		}else{
			validResult = false;
		}
	}

	@Override
	public List<SubjectOneResult> selectSubjectOneResult(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectOneResultMapper.selectSubjectOneResult(map);
	}
//	@Override
//	public void importExcelSubjectTowResult(Workbook wb, User user, HttpServletRequest request) {
//		// TODO Auto-generated method stub
//		StringBuilder failMessage = new StringBuilder();
//		StringBuilder errorinfo = new StringBuilder();
//		boolean validResult = true;
//		int successNum = 0;
//		int failNum = 0;
//		//验证第一行列名正确性		开始
//		Sheet sheet=wb.getSheetAt(0);
//		Row headRow = sheet.getRow(0);
//		if(headRow!=null){
//			if(cloumn.length==headRow.getLastCellNum()){//列数量匹配
//				for (int col = 0; col < headRow.getLastCellNum(); col++) {
//					if(!cloumn[col].equals(headRow.getCell(col).getStringCellValue().trim())){
//						validResult = false;
//					}
//				}
//			}else{
//				validResult = false;
//			}
//		}else{
//			validResult = false;
//		}
//	}


}
